package com.qingke.oms.command.shop;

import java.util.List;

import com.qingke.oms.command.ShopCommand;
import com.qingke.oms.pojo.Goods;
import com.qingke.oms.pojo.ShelfRecord;
import com.qingke.oms.pojo.ShelfStatus;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class OnShelfCommand extends ShopCommand implements UpdateGoodsStatistic{

	public OnShelfCommand(User user, Shop shop) {
		super(user, shop);
	}

	@Override
	public void execute() {
		if(user.getId()!=shop.getOwner().getId()){
			OMSConsole.println("It's not your shop,you can't put goods on shelf.");
			return ;
		}
		
		
		OMSConsole.println("search , on shelf or exit :[search],[on],[exit]");
		while(true){
			String cmd=OMSConsole.promptNotEmpty("do>").trim();
			if(cmd.equalsIgnoreCase("search")){
				searchGoods();
				continue;
			}
			else if(cmd.equalsIgnoreCase("on")){
				onShelf();
				continue;
			}
			else if(cmd.equalsIgnoreCase("exit")){
				OMSConsole.println("exit on shelf.");
				break ;
			}
			else if(cmd.equalsIgnoreCase("help")){
				OMSConsole.println("- search   input a part of code or name to search goods");
				OMSConsole.println("- on       choose goods on shelf");
				OMSConsole.println("- exit     exit on shelf");
				continue;
			}
			else{
				OMSConsole.println("err input.");
				OMSConsole.println("search , on shelf or exit :[search],[on],[exit]");
				continue;
			}
		}
		
	}
	public void searchGoods(){
		String key=OMSConsole.promptCanEmpty("search>");
		List<Goods> list=dao.selectGoodsByLikeCodeOrName(key);
		if(list.size()==0){
			OMSConsole.println("No query results");
			OMSConsole.println("you can input a part of code or name to search goods.");
			return ;
		}
		for(Goods g:list){
			OMSConsole.println(g.toString());
		}
	}
	public void onShelf(){
		int goodsIdInt=OMSConsole.getInt("goods id:");
		if(!dao.isGoodsExistById(goodsIdInt)){
			OMSConsole.println("The id of the goods is not existed.");
			return ;
		}
		Goods goods=dao.selectGoodsById(goodsIdInt);
		
		int goodsQuantity=OMSConsole.getInt("goods quantity:");
		if(goodsQuantity==0){
			OMSConsole.println("cancel on shelf.");
			return ;
		}
		user=dao.selectNewestUserById(user.getId());
		shop=dao.selectNewestShopById(shop.getId());
		ShelfRecord shelfRecord=new ShelfRecord();
		ShelfStatus shelfStatus=dao.selectShelfStatusByStatus("on shelf");
		shelfRecord.setGoods(goods);
		shelfRecord.setShop(shop);
		shelfRecord.setQuantity(goodsQuantity);
		shelfRecord.setShelfStatus(shelfStatus);
		
		boolean isSuccess=dao.insertShelfRecord(shelfRecord);
		updateGoodsStatistic();
		if(isSuccess)
			OMSConsole.println("On shelf success.");
		else
			OMSConsole.println("On shelf failed.");
		
		
	}

}
